package com.team233.zuccecho.controller;

import com.team233.zuccecho.dto.StudentDto;
import com.team233.zuccecho.dto.TeacherDto;
import com.team233.zuccecho.Bean.Student;
import com.team233.zuccecho.Bean.Teacher;
import com.team233.zuccecho.result.ExceptionMsg;
import com.team233.zuccecho.result.ResponseData;
import com.team233.zuccecho.service.inter.StudentInterface;
import com.team233.zuccecho.service.inter.TeacherInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.*;

@RestController
@RequestMapping("v1")
public class UserController {
    @Autowired
    private TeacherInterface teacherInterface;
    @Autowired
    private StudentInterface studentInterface;

    //用户登录
    @PostMapping("user/login")
    public ResponseData login(@RequestParam String user_id, @RequestParam String pwd){
        if(user_id.charAt(0)=='J') {
            String tea_id=teacherInterface.login(user_id,pwd);
            if(!tea_id.equals("")) return new ResponseData(ExceptionMsg.SUCCESS,tea_id);
        } else {
            long stu_id=studentInterface.login(Long.parseLong(user_id),pwd);
            if(stu_id!=-1) return new ResponseData(ExceptionMsg.SUCCESS,stu_id);
        }
        return new ResponseData(ExceptionMsg.FAILED,0);
    }

    //用户修改密码
    @PostMapping("user/modifyPwd")
    public ResponseData modifyPwdByTea(@RequestParam String user_id, @RequestParam String oldpwd
                                 ,@RequestParam String newpwd){
        if(user_id.charAt(0)=='J') {
            if(teacherInterface.modifyPwd(user_id,oldpwd,newpwd)==1) {
                return new ResponseData(ExceptionMsg.SUCCESS,"修改成功");
            }
        }
        else {
            if(studentInterface.modifyPwd(Long.parseLong(user_id),oldpwd,newpwd)==1) {
                return new ResponseData(ExceptionMsg.SUCCESS,"修改成功");
            }
        }
        return new ResponseData(ExceptionMsg.FAILED,"修改失败");
    }

    //管理员添加学生
    @PostMapping("admin/addStudent")
    public ResponseData addStudent(@RequestBody Map<String,List<StudentDto>> body){
        List<Student> stuList=studentInterface.addStu(body.get("students"));
        if(stuList!=null)  return new ResponseData(ExceptionMsg.SUCCESS,stuList);
        return new ResponseData(ExceptionMsg.FAILED,null);
    }


    //管理员添加教师
    @PostMapping("admin/addTeacher")
    public ResponseData addTeacher(@RequestBody Map<String,List<TeacherDto>> body){
        List<Teacher> teaList=teacherInterface.addTea(body.get("teachers"));
        if(teaList!=null) return new ResponseData(ExceptionMsg.SUCCESS,teaList);
        return new ResponseData(ExceptionMsg.FAILED,null);
    }
}
